package cn.hnu.qq.frame;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import cn.hnu.qq.client.controller.SendEmailController;
import cn.hnu.qq.entity.Crowd;
import cn.hnu.qq.entity.User;
import cn.hnu.qq.global.Email;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class SendEmailDialog extends JDialog implements ActionListener {

	private static SendEmailDialog instance = null;

	private SendEmailController controller = SendEmailController.getInstance();

	private final JPanel contentPanel = new JPanel(){
		@Override
		protected void paintComponent(Graphics g) {
			Image image = null;
			try {
				image = ImageIO.read(new File("src/main/java/image/02.jpg"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			};
					
			g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

		}
	};
	private JTextField senderField;
	private JTextField receiverField_1;
	private JTextField emailAccount;
	private JPasswordField emailPwd;
	private JTextArea emailContent;
	private JTextField subField;
	private JLabel receiverLabel;

	private User user;

	private Crowd crowd;

	private ArrayList<String> receriver = new ArrayList<String>();;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			SendEmailDialog dialog = new SendEmailDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static SendEmailDialog getInstance() {
		if (instance == null) {
			instance = new SendEmailDialog();
		}
		return instance;
	}

	/**
	 * Create the dialog.
	 */
	private SendEmailDialog() {
		try {
			setIconImage(ImageIO.read(new File("src/main/java/image/xiaohui.jpg")));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		setBounds(100, 100, 558, 469);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JPanel panel = new JPanel();
		panel.setBounds(10, 10, 526, 181);
		contentPanel.add(panel);
		panel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"), }, new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, }));

		JLabel label = new JLabel("发件人：");
		panel.add(label, "6, 2");

		senderField = new JTextField();
		panel.add(senderField, "10, 2, left, default");
		senderField.setColumns(50);
		senderField.setEditable(false);
		receiverLabel = new JLabel("收件人：");
		panel.add(receiverLabel, "6, 4");

		receiverField_1 = new JTextField();
		panel.add(receiverField_1, "10, 4, left, default");
		receiverField_1.setColumns(50);
		receiverField_1.setEditable(false);

		JLabel label_2 = new JLabel("主题：");
		panel.add(label_2, "6, 6");

		subField = new JTextField();
		panel.add(subField, "10, 6, left, default");
		subField.setColumns(50);

		JLabel label_3 = new JLabel("发件箱设置");
		panel.add(label_3, "10, 8");

		JLabel label_4 = new JLabel("邮箱账号：");
		panel.add(label_4, "6, 10");

		emailAccount = new JTextField();
		panel.add(emailAccount, "10, 10, left, default");
		emailAccount.setColumns(50);

		JLabel label_5 = new JLabel("邮箱登陆密码：");
		panel.add(label_5, "6, 12");

		emailPwd = new JPasswordField();
		emailPwd.setColumns(50);
		panel.add(emailPwd, "10, 12, left, default");

		JPanel panel_1 = new JPanel();
		panel_1.setBounds(10, 201, 530, 201);
		contentPanel.add(panel_1);
		panel_1.setLayout(new BorderLayout(0, 0));

		JLabel lblsmtp = new JLabel("请开通相应邮箱的SMTP功能，否则邮件将发送失败");
		lblsmtp.setHorizontalAlignment(SwingConstants.CENTER);
		panel_1.add(lblsmtp, BorderLayout.NORTH);

		JScrollPane scrollPane = new JScrollPane();
		panel_1.add(scrollPane, BorderLayout.CENTER);

		emailContent = new JTextArea();
		scrollPane.setViewportView(emailContent);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(this);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(this);
				buttonPane.add(cancelButton);
			}
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("OK")) {
			if (emailAccount.getText() == null
					|| emailAccount.getText().equals("")) {
				JOptionPane.showMessageDialog(this, "邮箱账号不能为空");
				return;
			}
			if (subField.getText() == null || subField.getText().equals("")) {
				JOptionPane.showMessageDialog(this, "主题不能为空");
				return;
			}
			String pwd = new String(emailPwd.getPassword());
			if (pwd.equals("")) {
				JOptionPane.showMessageDialog(this, "密码不能为空");
				return;
			}
			Email email = new Email();
			email.content = emailContent.getText();
			email.account = emailAccount.getText();
			email.password = pwd;
			email.sub = subField.getText();
			email.sender = senderField.getText();
			email.receiver = receiverField_1.getText();

			try {
					controller.sendEmail(receriver, email);
					JOptionPane.showMessageDialog(this, "邮件发送成功");
					this.dispose();
			} catch (Exception ex) {
				JOptionPane.showMessageDialog(this, "邮件发送失败");
			}

		}
		if (e.getActionCommand().equals("Cancel")) {
			this.dispose();
		}
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Crowd getCrowd() {
		return crowd;
	}

	public void setCrowd(Crowd crowd) {
		this.crowd = crowd;
	}


	public void setSender(Crowd crowd) {
		StringBuffer sb = new StringBuffer();
		receriver.clear();
		Set<User> users = crowd.getUsers();
		for(User u:users){
			sb.append(u.getEmail()).append(";");
			receriver.add(u.getEmail());
		}
		senderField.setText(controller.getCurUser().getEmail());
		senderField.validate();
		receiverField_1.setText(sb.toString());
		receiverField_1.validate();
	}

	public void setSender(User user) {
		receriver.clear();
		receriver.add(user.getEmail());
		senderField.setText(controller.getCurUser().getEmail());
		senderField.validate();
		receiverField_1.setText(user.getEmail());
		receiverField_1.validate();
	}
}
